package cuit.demo.util;

import java.sql.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class DBUtil {
    private static final String url = "jdbc:mysql://localhost:3306/boke"; // 根据你的设置更改 localhost 和 3306
    private static final String user = "root"; // 替换为你的用户名
    private static final String password = "123456";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * \
     * @return
     */
    public static Connection getConnection(){
        Connection connection = null;
        try {
           connection = DriverManager.getConnection(url, user, password);
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
        System.out.println("Connecting to database with URL: " + url);
        return connection;
    }

    /**
     *
     * @param resultSet
     * @param preparedStatement
     * @param connection
     */
    public  static  void  close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection){
        try{
            if(resultSet != null){
                resultSet.close();
            }
            if(preparedStatement != null){
                preparedStatement.close();
            }
            if(connection != null){
                connection.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
